let ani1 = null;
let ani2 = null;
const api = require('../../utils/interface.js')
const math = require("../../utils/math.js")
const tools = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabIndex:1,
    rechargeLsit:[],
    consumptionLsit:[],
    cardId:'',
    state1:1,
    state2:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarColor({
      frontColor: '#000000',
      backgroundColor: "#ffffff"
    })
    console.log(options,"idddd")
    this.data.cardId = options.cardId
    this.getRechargeList()
    this.getMemberCardOrders()
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];
    prevPage.setData({
      isShow: false
    })
  },
  // 切换消费充值列表
  select(e){
    let index = e.currentTarget.dataset.index;
    this.setData({
      tabIndex: index
    })
  },
  // 日期展开收起
  start(e){
    console.log(e)
    let height = e.currentTarget.dataset.height
    let index = e.currentTarget.dataset.index
    if (this.data.rechargeLsit[index].state){
      ani1.height(0).step()
      ani2.rotate(0).step()
      this.data.rechargeLsit[index].animate = ani1.export()
      this.data.rechargeLsit[index].rotate = ani2.export()
      this.data.rechargeLsit[index].state = false
    }else{
      ani2.rotate(180).step()
      ani1.height(height + 'rpx').step()
      this.data.rechargeLsit[index].rotate = ani2.export()
      this.data.rechargeLsit[index].animate = ani1.export()
      this.data.rechargeLsit[index].state = true
    }
    this.setData({
      rechargeLsit: this.data.rechargeLsit
    })
  },
  startTo(e){
    let height = e.currentTarget.dataset.height
    let index = e.currentTarget.dataset.index
    if (this.data.consumptionLsit[index].state){
      ani1.height(0).step()
      ani2.rotate(0).step()
      this.data.consumptionLsit[index].animate = ani1.export()
      this.data.consumptionLsit[index].rotate = ani2.export()
      this.data.consumptionLsit[index].state = false
    }else{
      ani2.rotate(180).step()
      ani1.height(height + 'rpx').step()
      this.data.consumptionLsit[index].rotate = ani2.export()
      this.data.consumptionLsit[index].animate = ani1.export()
      this.data.consumptionLsit[index].state = true
    }
    this.setData({
      consumptionLsit: this.data.consumptionLsit
    })
  },
  // 跳转消费订单详情
  go_consumptionOrderInfo(e){
    let orderNumber = e.currentTarget.dataset.ordernumber
    wx.navigateTo({
      url: './consumptionOrderInfo/consumptionOrderInfo?orderNumber=' + orderNumber,
    })
  },
  // 跳转充值订单详情
  go_rechargeOrderInfo(e){
    let orderNumber = e.currentTarget.dataset.ordernumber
    wx.navigateTo({
      url: './rechargeOrderInfo/rechargeOrderInfo?orderNumber=' + orderNumber,
    })
  },
  // 获取充值订单数据
  getRechargeList(){
    let user = wx.getStorageSync('userInfo')
    api.getROrders({ page: 1, memberCardId: this.data.cardId, pageSize: 999999}).then(res=>{
      if (res.data.result&&res.data.result.length){
        this.setData({
          state2:2
        })
      }else{
        this.setData({
          state2: 3
        })
      }
      res.data.result.map(item=>{
        item.animate = null
        item.rotate = null
        item.height = 169*item.data.length
        item.state = true
        item.data.map(item=>{
          item.amount_ = math.divide(item.amount,100)
          item.date = tools.formatTime(new Date(item.createTime))
        })
      })
      this.setData({
        rechargeLsit: res.data.result
      })
    })
  },
  // 获取消费订单数据
  getMemberCardOrders(){
    api.getMemberCardOrders({ page: 1, memberCardId: this.data.cardId, pageSize: 999999 }).then(res=>{
      if (res.data.result && res.data.result.length) {
        this.setData({
          state1: 2
        })
      } else {
        this.setData({
          state1: 3
        })
      }
      res.data.result.map(item => {
        item.animate = null
        item.rotate = null
        item.height = 169 * item.data.length
        item.state = true

        item.data.map(item => {
          item.amount_ = math.divide(item.amount, 100)
          item.date = tools.formatTime(new Date(item.createTime))
        })
      })
      this.setData({
        consumptionLsit: res.data.result
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    ani1 = wx.createAnimation({
      duration: 500,
      delay: 100,
      timingFunction: 'ease',
    })
    ani2 = wx.createAnimation({
      duration: 500,
      delay: 100,
      timingFunction: 'ease',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  onUnload() {
    console.log('返回按钮')
    wx.navigateBack({
      delta: 1
    })
  }

 
})